﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore.Controls
 * 唯一标识：603e8688-274a-40b0-8804-0e05b15ecac2
 * 文件名：Space
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/6/26 11:30:57
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.BlogCore;

/// <summary>
/// 间距控件
/// </summary>
public sealed class Space : ContainerControlBase
{
    /// <summary>
    /// 构造函数
    /// </summary>
    internal Space()
    {
    }

    public override string Title { get; set; }
    public override List<ControlBase> Children { get; internal set; }
    public override string Id { get; set; }

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        Param par = new Param();
        par.Add($"size=\"{Size}\"");

        if (IsWrap) par.Add($"wrap");

        if (IsFill)
        {
            par.Add($"fill=\"true\"");

            par.Add($"fill-ratio=\"{FillRadio}\"");
        }

        par.Add($"direction=\"{Direction.GetDescription()}\"");

        if (Alignment != SpaceAlignmentType.Center) par.Add($"alignment=\"{Alignment.GetDescription()}\"");

        if (!string.IsNullOrEmpty(Spacer)) par.Add($"spacer=\"{Spacer}\"");

        sb.AppendLine($"<el-space {par}>");

        foreach (var item in Children)
        {
            sb.AppendLine(item.ConverToHtml());
        }

        sb.AppendLine($"</el-space>");

        return sb.ToString();
    }

    /// <summary>
    /// 对齐方式
    /// </summary>
    internal SpaceAlignmentType Alignment { get; set; }

    /// <summary>
    /// 排列方式
    /// </summary>
    internal DirectionType Direction { get; set; }

    /// <summary>
    /// 间隔符号
    /// </summary>
    internal string Spacer { get; set; }

    /// <summary>
    /// 间隔大小
    /// </summary>
    internal int Size { get; set; }

    /// <summary>
    /// 是否自动拆行
    /// </summary>
    internal bool IsWrap { get; set; }

    /// <summary>
    /// 子元素是否填充父容器
    /// </summary>
    internal bool IsFill { get; set; }

    /// <summary>
    /// 填充父容器的比例
    /// </summary>
    internal int FillRadio { get; set; }
}

public static partial class App
{
    /// <summary>
    /// 创建间隔容器控件
    /// </summary>
    /// <param name="child">内容</param>
    /// <param name="alignment">对齐方式</param>
    /// <param name="direction">排列方式</param>
    /// <param name="spacer">间隔符号</param>
    /// <param name="size">间隔大小</param>
    /// <param name="isWarp">是否自动拆行</param>
    /// <param name="isFill">子元素是否填充父容器</param>
    /// <param name="fillRadio">填充父容器的比例</param>
    /// <returns></returns>
    public static Space CreateSpace(Action<List<ControlBase>> child, SpaceAlignmentType alignment = SpaceAlignmentType.Normal, DirectionType direction = DirectionType.Horizontal, string spacer = "", int size = 5, bool isWarp = false, bool isFill = false, int fillRadio = 100)
    {
        if (fillRadio > 100) fillRadio = 100;

        if (fillRadio < 0) fillRadio = 0;

        if (size < 0) size = 0;

        Space space = new Space()
        {
            Id = GenerateId(),
            Title = "Space",
            Children = new List<ControlBase>(),
            Alignment = alignment,
            Direction = direction,
            Spacer = spacer,
            Size = size,
            IsWrap = isWarp,
            IsFill = isFill,
            FillRadio = fillRadio
        };

        child?.Invoke(space.Children);

        return space;
    }
}

/// <summary>
/// 对齐方式
/// </summary>
public enum SpaceAlignmentType
{
    /// <summary>
    ///
    /// </summary>
    [Description("center")]
    Center,

    /// <summary>
    ///
    /// </summary>
    [Description("normal")]
    Normal,

    /// <summary>
    ///
    /// </summary>
    [Description("stretch")]
    Stretch
}

/// <summary>
/// 排列方式
/// </summary>
public enum DirectionType
{
    /// <summary>
    /// 纵向
    /// </summary>
    [Description("vertical")]
    Vertical,

    /// <summary>
    /// 横向
    /// </summary>
    [Description("horizontal")]
    Horizontal
}